<?php
/**
 * @package EXCEL
 * @copyright Авторские права (C) 2010 www.yar-it.com. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * Price Excel! - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * Для получения информации о используемых расширениях и замечаний об авторском праве, смотрите файл copyright.php.
 */


defined( '_JEXEC' ) or die( 'Restricted access' );
global $mainframe;
//error_reporting(E_ALL ^ E_NOTICE);

$database = &JFactory::getDBO();
if (!defined( 'DS' )) {
    define( 'DS', DIRECTORY_SEPARATOR );
}

$catg     = intval( JArrayHelper::getValue($_REQUEST, 'catg', 0) );
$prslst   = intval( JArrayHelper::getValue($_REQUEST, 'prslst', 0) );
//$Itemid   = intval( JArrayHelper::getValue( $_REQUEST, 'Itemid', '' ) );
$Itemid =  JRequest::getInt('Itemid',0);
$jxlspppath  = JURI::base().'components/com_excel';

//загрузка скриптов и стилей
$header = '';
$header .= "<link href=\"$jxlspppath"."/styles/jxlstyle.css\" rel=\"stylesheet\" type=\"text/css\" />\n";

JHTML::_('behavior.mootools');
$header .= "<script type=\"text/javascript\" src=\"$jxlspppath/js/slimbox/js/slimbox.js\"></script>\n";
$header .= "<link rel=\"stylesheet\" href=\"$jxlspppath/js/slimbox/css/slimbox.css\" type=\"text/css\" media=\"screen\" />\n";
$mainframe->addCustomHeadTag($header);

echo "\n<script type=\"text/javascript\">\n";
echo "window.addEvent('domready', function(){\n";
echo "window.addEvent('domready', Lightbox.init.bind(Lightbox));\n";
echo "});";
echo "</script>\n";

$catg = JArrayHelper::getValue($_REQUEST, 'catg', '');

require(JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_excel'.DS.'config.excel.php');

$query = "select cid, name, descript from #__price_excel_cat where published=1 order by cid asc";
$database->setQuery($query);
$catl = $database->loadObjectList();

if(is_array($catl)) {
    foreach ($catl as $catlg) {
        $link = null;
        $link = JRoute::_( 'index.php?option=com_excel&amp;Itemid='.$Itemid.'&amp;catg='.$catlg->cid);
        echo '<a href="'.$link.'">'.$catlg->name.'</a> ['.GetNumberOfLinks ($catlg->cid).']<br/>';
        echo '<div id="catdesc">'.$catlg->descript.'</div>';
        if ($catg == $catlg->cid) {
            $quep = 'SELECT * FROM #__price_excel WHERE catid='.$catlg->cid.' AND published=1 ORDER BY id asc';
            $database->setQuery($quep);
            $prsl = $database->loadObjectList();

            if(is_array($prsl)) {
                foreach ($prsl as $prslg) {
                    $link = JRoute::_('index.php?option=com_excel&amp;Itemid='.$Itemid.'&amp;catg='.$catlg->cid.'&amp;prslst='.$prslg->id);
                    echo '<div class="prslink"><a href="'.$link.'">'.$prslg->title.'</a><font size="2"> - <i> '. JText::_('pex last_update') .' </i>'.strftime("%d.%m.%Y %H:%M",$prslg->date).'</font></div>';
                }
                //	$curpath[0] = $catlg->cid;
                //	$curpath[1] = $catlg->name;
            } else {
                echo JText::_('pex noitem');
            }
        }
    }

}


if ($prslst !== 0) {
    $database->setQuery('select * from #__price_excel where id='.$prslst.' and published=1');
    $prsl = $database->query();
    $num_rows = mysql_num_rows($prsl);
    $pfile = mysql_result($prsl,0,'filename');
    $excel_file = JPATH_SITE.DS.$jx_path.DS.$pfile;
    echo '<br />';
    include('excel.excel.show.php');
} 

if (!($catg == null) & ($prslst == null)) {
    $mainframe->AppendPathway($catlg->name);
}
if ($prslst !== null) {

    $mainframe->AppendPathway($catlg->name, 'index.php?option=com_excel&amp;Itemid='.$Itemid.'&amp;catg='.$catlg->cid);
    //$mainframe->AppendPathway($prslg->title);
}

function GetNumberOfLinks ($cat) {

    $database = &JFactory::getDBO();
    $queue[] = intval($cat);
    //reset($queue);
    $query = "select count(*) from #__price_excel  where ( 0!=0";
    while (list($key, $cat) = each($queue)) {
        $query .= " or catid = $cat";
    }
    $query = $query . " ) and published=1";
    $database->setQuery($query);
    $result = $database->query();
    $val = mysql_fetch_row($result);
    return $val[0];
}
if ($jx_cr_on !== 0) {

    echo '<div class="smallgrey" align="right">'.$jx_cr.'</div>';
} else {
    echo '<div class="smallgrey" align="right" style="display:none">'.$jx_cr.'</div>';
}
?>
